/*
 * @Author: szx
 * @Date: 2022-05-18 13:20:42
 * @LastEditTime: 2022-05-18 13:25:14
 * @Description:
 * @FilePath: \leetcode\面试题\04\04\04.js
 */
function TreeNode(val) {
    this.val = val;
    this.left = this.right = null;
}

/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function (root) {
    const dfs = (root) => {
        if (!root) return 0;
        const left = dfs(root.left);
        const right = dfs(root.right);
        if (left == -1 || right == -1 || Math.abs(left - right) > 1) return -1;
        return Math.max(left, right) + 1;
    };
    return dfs(root) >= 1;
};
